﻿/*****************************************介绍*****************************************
 * 作者：Ywh
 * 创建时间：2025-05-17 15:19:25
 * 功能：管理所有UI面板
 *      1、注册
 *      2、注销
 *      3、获取
 *      4、创建/显示
 *      5、关闭
 *      6、清理
**************************************************************************************/

using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace Game.Framework
{
    [InternalModule(7)]
    public class PanelManager : BaseManager<PanelManager>, IModule
    {
        private InitResult m_InitResult;
        private Dictionary<string, BasePanel> m_Panels;

        private PanelManager() { }

        void IModule.Init()
        {
            m_InitResult = InitResult.Wait;
            m_Panels = new Dictionary<string, BasePanel>();
            m_InitResult.type = E_ResultType.Success;
        }

        InitResult IModule.GetInitResult() => m_InitResult;

        void IModule.Clear()
        {
            if (m_Panels == null) return;
            var panels = m_Panels.Values.ToArray();
            foreach (var item in panels)
            {
                item.Clear();
            }
            m_Panels.Clear();
        }

        public void RegistPanel(string key, BasePanel panel)
        {
            if (m_Panels.TryAdd(key, panel))
                LogManager.Log($"regist panel, key:{key} total:{m_Panels.Count}", "PanelManager", "Regist");
            else
                LogManager.LogWarning($"repeat regist panel！！ key:{key} total:{m_Panels.Count}", "PanelManager", "Regist");
        }

        public void UnregistPanel(string key)
        {
            if (m_Panels.ContainsKey(key))
            {
                m_Panels.Remove(key);
                LogManager.Log($"unregist panel, key:{key} total:{m_Panels.Count}", "PanelManager", "Unregist");
            }
        }

        public T GetPanel<T>(string key) where T : BasePanel
        {
            if (m_Panels.ContainsKey(key))
            {
                return m_Panels[key] as T;
            }
            return null;
        }

        private Transform GetPanelParent(UIRoot root)
        {
            switch (root)
            {
                case UIRoot.Canvas:
                    return GameObject.Find("/Canvas").transform;
                case UIRoot.Top:
                    return GameObject.Find("/Canvas/Top").transform;
                case UIRoot.MostTop:
                    return GameObject.Find("/Canvas/MostTop").transform;
                default:
                    return null;
            }
        }

        public void DisplayPanel(string key, string path, UIRoot root = UIRoot.Canvas, object data = null)
        {
            var panel = GetPanel<BasePanel>(key);
            if (panel == null)
            {
                var prefab = Resources.Load<GameObject>(path);
                if (prefab == null)
                {
                    LogManager.LogError($"can not find panel:{path}", "PanelManager", "Display");
                    return;
                }
                panel = Object.Instantiate(prefab, GetPanelParent(root)).GetComponent<BasePanel>();
                panel.name = prefab.name;
                panel.transform.SetAsLastSibling();
                panel.Init(data);
            }
            else
            {
                if (panel.gameObject.activeSelf)
                    HidePanel(key);
                else
                    ShowPanel(key, data);
            }
        }

        public void ShowPanel(string key, object data = null)
        {
            var status = GetPanelState(key);
            if (status == PanelState.Hide)
            {
                var panel = GetPanel<BasePanel>(key);
                panel.transform.SetAsLastSibling();
                panel.gameObject.SetActive(true);
                panel.Init(data);
            }
        }

        public void HidePanel(string key)
        {
            var status = GetPanelState(key);
            if (status == PanelState.Show)
            {
                var panel = GetPanel<BasePanel>(key);
                panel.Hide();
            }
        }

        public void DestroyPanel(string key)
        {
            var status = GetPanelState(key);
            if (status != PanelState.None)
            {
                var panel = GetPanel<BasePanel>(key);
                panel.Hide(true);
            }
        }

        /// <summary>获取指定界面的状态</summary>
        public PanelState GetPanelState(string key)
        {
            PanelState result = PanelState.None;
            var panel = GetPanel<BasePanel>(key);
            if (panel != null)
                result = panel.gameObject.activeSelf ? PanelState.Show : PanelState.Hide;
            return result;
        }
    }

    public enum UIRoot
    {
        Null,
        Canvas,
        Top,
        MostTop,
    }

    public enum PanelState
    {
        None = 0,   // 未创建
        Hide = 1,   // 隐藏
        Show = 2,   // 已打开
    }
}